from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *

from ui.Ui_private_script_searcher_dialog import Ui_Dialog_private_script_searcher



class Search_signal(QObject):
    search_script = Signal(dict)



class Private_script_searcher_dialog(QDialog):
    
    
    def __init__(self, parent=None):
        super(Private_script_searcher_dialog, self).__init__(parent)
        self.ui = Ui_Dialog_private_script_searcher()
        self.ui.setupUi(self)

        self.setModal(True)  # 设置窗体为模态窗口，必须为True
        self.setWindowFlags(Qt.WindowCloseButtonHint)  # 设置窗体仅显示关闭按钮
        
        self.signal = Search_signal()
        self.init_col_names()
        self.init_item()

        self.ui.pushButton.clicked.connect(self.search_script)

        self.ui.tableWidget.horizontalHeader().setStretchLastSection(True)  #设置最后一行自适应宽度
    def init_item(self):
        max_col = self.ui.tableWidget.columnCount()
        for i in range(max_col):
            self.ui.tableWidget.setItem(0,i, QTableWidgetItem(''))



    def init_col_names(self):
        self.col_names = {}
        # 获取列id
        max_col = self.ui.tableWidget.columnCount()
        for i in range(max_col):
            text = self.ui.tableWidget.horizontalHeaderItem(i).text()
            self.col_names[text] = i


    def search_script(self):
        filter = {
            'name' : self.ui.tableWidget.item(0,self.col_names['名称']).text(),
            'supplier' : self.ui.tableWidget.item(0,self.col_names['适配厂商']).text(),
            'model' : self.ui.tableWidget.item(0,self.col_names['适配型号']).text(),
            'info' : self.ui.tableWidget.item(0,self.col_names['描述']).text(),
        }
        self.signal.search_script.emit(filter)

    def show_script(self,device_id):
        filter = {
            'device_id' : device_id,
        }
        self.signal.search_script.emit(filter)